<?php

	include_once ( "AbstractDAO.php" );

	class ProductCategoryDAO extends AbstractDAO 
	{
		//Get Level 1 product Category from table product_category
		function GetLevel1Category()
		{
			$cacheManager = GetCacheManager();
			$re = $cacheManager->Get( "topLevelProductCate", CNS_INDEX );
			if( $re !== false )
				return $re;
				
			$result = $this->db->GetPlan( "select id, cate_name from product_category where parent_id=0" );

			$productCateList = array();
			if( $result ){
				foreach ( $result as $item )
				{
					$productCateList[$item['cate_name']]=$item['id'];
				}
			}
			$cacheManager->Set( "topLevelProductCate", $productCateList, CNS_INDEX );
		    return $productCateList;
		}
		
		//Map from product category id to category name
		function GetProductCategoryName($pcid)
		{
			if($pcid=='0')
				return '';//取不到显示为空
			else
				return $this->db->GetValue("select cate_name from product_category where id=".$pcid);
		}

		/**
		 * return category names by category id list
		 * @param array $ids
		 * @return array
		 */
		function GetProductCateInfoByIdList( $ids ){
			$strCateIds = implode( ",", $ids );
			$result = $this->db->GetPlan( "select id, cate_name from product_category where id in (".$strCateIds.") ORDER BY FIELD(id, ".$strCateIds.")" );

			return $result;
		}
		
		function ConvertTaobaoProductCid($taobao_cid){
			
			$result=$this->db->GetRow("select id, parent_id from product_category where taobao_cid=".$taobao_cid);
			
			if($result){
				
				if($result['parent_id']=='0')
				{
					return $result['id'];
				}
				while($result['parent_id']!='0')
				{
					$result=$this->db->GetRow("select id, parent_id from product_category where id=".$result['parent_id']);
				}
				return $result['id']; 				
	
			}else
			   return '0';//找不到返回0
	    }
	    
	    
		function GetProductCategoryIDByName($cateName)
		{
			return $this->db->GetValue("select id from product_category where cate_name='".$cateName."' and parent_id=0");
		}	    
			
	}
	
	

?>